{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# lambda表达式\n",
    "name = lambda 参数表列：运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "add = lambda x,y:x+y\n",
    "add(1,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# map函数\n",
    "map函数的第一个参数是一个函数，后面的参数等于是给这个函数赋值，但不仅仅限于一个值，可以是（需要几个就是几个）可迭代对象，这样map函数就会把所有可迭代对象都放到函数中运算一遍，返回所有结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "result1 = map(lambda x:x+x,[1,2,3,4])\n",
    "result2 = map(lambda x,y:x+y,[1,2,3,4],[10,11,12,13])\n",
    "print(list(result1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# filter函数\n",
    "filter函数和map函数的作用类似，但是map函数返回的是计算结果，filter函数会先输入后面的迭代对象进行计算，如果结果是false或0，则判断为舍弃，返回时不会保留这个输入值，如果计算结果是其他值，就保留，返回原输入值，请看以下例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 4]\n",
      "[1, 3]\n",
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "result1 = filter(lambda x:x-2,[1,2,3,4]) #这里2-2=0，判断为false，不返回2，就等于把2筛选出去了\n",
    "print(list(result1))\n",
    "result = filter(lambda x:x%2,[1,2,3,4])  #这里模除2和4的结果都是0，所以返回了1和3\n",
    "print(list(result))\n",
    "result2 = filter(lambda x:x/2,[1,2,3,4]) #这里因为没有结果等于false或者0，所以就都返回了\n",
    "print(list(result2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# reduce函数\n",
    "这是一个累计操作函数，就是说，每次执行函数都是上一次的结果和参数中的值进行计算,注意参数中给定的操作必须只有两个参数，不然无法操作，最后一个操作可以定义初始值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "result1 = reduce(lambda x,y:x-y,[1,2,3,4],20) \n",
    "print(result1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 闭包\n",
    "返回值是一个函数的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<function func1.<locals>.func2 at 0x0000023CF0505F28>\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "def func1():\n",
    "    def func2():\n",
    "        return 0\n",
    "    return func2\n",
    "\n",
    "f = func1()\n",
    "print(f)\n",
    "print(f())\n",
    "\n",
    "#这里的层次关系是，f为func1()的返回值，其实就是func2，所以我们在执行f的时候，得到的是一个函数的信息\n",
    "#必须执行f()才会返回func2的返回值，这就说明f其实是一个函数的指针，指向的func2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 装饰器，语法糖，注解\n",
    "把一个函数当作参数传递给另一个参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "运行了func1\n",
      "运行了func2\n"
     ]
    }
   ],
   "source": [
    "def func1(func):\n",
    "    print(\"运行了func1\")\n",
    "    def funcin(*args,**kwargs):\n",
    "        func(*args,**kwargs)\n",
    "    return funcin\n",
    "\n",
    "@func1 #这样的写法就说明func2要被传入func1中\n",
    "def func2(*args,**kwargs):\n",
    "    print(\"运行了func2\")\n",
    "    \n",
    "func2(1,2,3,4,i = 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.0 64-bit ('base': conda)",
   "language": "python",
   "name": "python37064bitbaseconda2143ef630c4a49348782c142bbb4c59a"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
